package richbar.com.richbar.view.pop;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.help.RecycleViewHelp;
import com.common.util.DisplayUtil;
import com.common.util.LogUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.AddressResult;
import richbar.com.richbar.bean.Bean;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/12/7.
 */

public class CommonBottomPop {
    private Activity context;
    private PopupWindow popupWindow;
    private MyAdapter adapter;

    public CommonBottomPop(Activity context) {
        this.context = context;
    }

    public void show(View parent,String title,List<Bean> data, Callback callback){
        //准备PopupWindow的布局View
        View view = LayoutInflater.from(context).inflate(R.layout.pop_common_bottom, null);
        ImageView close = view.findViewById(R.id.close);
        TextView title_tv = view.findViewById(R.id.title);
        title_tv.setText(title);
        RecyclerView recyclerView = view.findViewById(R.id.recycleView);
        adapter = new MyAdapter(R.layout.item_commont_pop,data,callback);
        RecycleViewHelp.setGridMannager(recyclerView, DisplayUtil.dp2px(context,5),2,context);
        recyclerView.setAdapter(adapter);
        popupWindow = new PopupWindow(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (context != null) {
                    setBackgroundAlpha(context, 1.0f);
                }
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        LogUtil.e("PPAPAPAPPA","1");
        if (null != popupWindow && !popupWindow.isShowing()){
            LogUtil.e("PPAPAPAPPA","2");
            popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);
        }
        setBackgroundAlpha(context,0.5f);//设置屏幕透明度
    }

    public void click(final OnItemOnClickListener listener){
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                listener.onItemClick(i);
            }
        });
    }



    private void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1.0f) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }


    class MyAdapter extends BaseQuickAdapter<Bean> {
        private int clickPos = -1;
        private Callback callback;

        public MyAdapter(int layoutResId, List<Bean> data,Callback callback) {
            super(layoutResId, data);
            this.callback = callback;
        }

        @Override
        protected void convert(final BaseViewHolder holder, final Bean bean) {
            TextView tv = holder.getView(R.id.tv);
            if (clickPos == holder.getAdapterPosition()) {
                tv.setTextColor(ContextCompat.getColor(context,R.color.white));
                tv.setBackgroundResource(R.mipmap.red_bg);
            } else {
                tv.setTextColor(ContextCompat.getColor(context,R.color.color3));
                tv.setBackgroundResource(R.drawable.shape_gray);
            }
            holder.setText(R.id.tv, bean.getValue());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickPos = holder.getAdapterPosition();
                    notifyDataSetChanged();
                    callback.callback(bean);
                    popupWindow.dismiss();
                }
            });


        }
    }

    public interface Callback{
        void callback(Bean bean);
    }

    public interface OnItemOnClickListener{
        void onItemClick(int pos);
    }
}
